package com.hengpeng.itfin.tools;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.Date;

import com.hengpeng.itfin.common.Configuration;
import com.hengpeng.itfinbase.constants.SystemConfigConstants;
import com.hengpeng.itfinbase.persist.UserInfo;

public class StringUtils {
    public static double multiply(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.multiply(b2).doubleValue();
    }

    public static String sicenToComm(double value) {
        NumberFormat nf = NumberFormat.getInstance();
        nf.setGroupingUsed(false);
        return nf.format(value);
    }

    /**
     * <pre>
     * double除法运算，精确到小数点后面2位小数，以后的数字四舍六入
     * </pre>
     * 
     * @param v1
     * @param v2
     * @return
     */
    public static double divide(double v1, double v2) {
        return divide(v1, v2, 2, BigDecimal.ROUND_HALF_EVEN);
    }

    /**
     * <pre>
     * double除法运算，精确到小数点后面scale位小数，以后的数字四舍六入
     * </pre>
     * 
     * @param v1
     * @param v2
     * @return
     */
    public static double divide(double v1, double v2, int scale) {
        return divide(v1, v2, scale, BigDecimal.ROUND_HALF_EVEN);
    }

    /**
     * <pre>
     * 除法运算，指定精度
     * </pre>
     * 
     * @param v1
     * @param v2
     * @param scale
     * @param round_mode
     * @return
     */
    public static double divide(double v1, double v2, int scale, int round_mode) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.divide(b2, scale, round_mode).doubleValue();
    }

    /**
     * <pre>
     * 浮点数的减法运算
     * </pre>
     * 
     * @param v1
     * @param v2
     * @return
     */
    public static double sub(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.subtract(b2).doubleValue();
    }

    public static String getUniteCreater(String userName, Integer gameId, Long planId) {
        String url = Configuration.getValue("unite.plan");
        return ",发起人为:<a class='red' href='" + url + "?gameId=" + gameId + "&planId=" + planId + "' >" + userName
                + "</a>";
    }

    /**
     * <pre>
     * 科学计数法转化为double
     * </pre>
     * 
     * @param science
     *            (例如：1.23452809E8)
     * @return
     */
    public static double getDoubleByScience(String science) {
        BigDecimal d = new BigDecimal(science);
        return d.doubleValue();
    }

    /**
     * <pre>
     * 科学计数法转化为long
     * </pre>
     * 
     * @param science
     *            (例如：1.23452809E8)
     * @return
     */
    public static long getLongByScience(String science) {
        BigDecimal d = new BigDecimal(science);
        return d.longValue();
    }

    /**
     * <pre>
     * 科学计数法转化为int
     * </pre>
     * 
     * @param science
     *            (例如：1.23452809E8)
     * @return
     */
    public static int getIntegerByScience(String science) {
        BigDecimal d = new BigDecimal(science);
        return d.intValue();
    }

    /**
     * <pre>
     * 将整型数据转换为4位的字节数组
     * </pre>
     * 
     * @param num
     * @return
     */
    public static byte[] intToBytes(int num) {
        byte[] b = new byte[4];
        for (int i = 0; i < 4; i++) {
            b[i] = (byte) (num >>> (24 - i * 8));
        }
        return b;
    }

    /**
     * <pre>
     * 将字节数组转换为整型
     * </pre>
     * 
     * @param bytes
     * @return
     */
    public static int bytesToInt(byte[] bytes) {
        int value = 0;
        // 由高位到低位
        for (int i = 0; i < 4; i++) {
            int shift = (4 - 1 - i) * 8;
            value += (bytes[i] & 0x000000FF) << shift;// 往高位游
        }
        return value;
    }

    /**
     * <pre>
     * 合并两个byte数组
     * </pre>
     * 
     * @param byte1
     * @param byte2
     * @return
     */
    public static byte[] byteMerger(byte[] byte1, byte[] byte2) {
        byte[] byte_3 = new byte[byte1.length + byte2.length];
        System.arraycopy(byte1, 0, byte_3, 0, byte1.length);
        System.arraycopy(byte2, 0, byte_3, byte1.length, byte2.length);
        return byte_3;
    }

    /**
     * <pre>
     * 格式化数字，不足则前面补0
     * </pre>
     * 
     * @author chenhui
     * @param pitchId
     * @param length
     *            长度
     * @return
     */
    public static String format(String pitchId, int length) {
        String format = pitchId;
        if (pitchId != null && !pitchId.trim().equals("")) {
            for (int i = 0; i < length - pitchId.length(); i++) {
                format = "0" + format;
            }
        }
        return format;
    }

    /**
     * 获得某一日期的后几天
     * 
     * @param date
     *            当天日期
     * @param days
     *            第几天
     * @return Date 第二天12点
     */
    public static Date getNextDate(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, days);
        calendar.set(Calendar.HOUR_OF_DAY, 12);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * <pre>
     * 添加日期
     * </pre>
     * 
     * @param date
     * @param num
     * @param type
     * @return
     */
    public static Date addTime(Date date, int num, int type) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(type, num);
        return calendar.getTime();
    }

    /**
     * 生成随机字母
     */
    public static String getRecommendCode(int num) {
        String recommendCode = "";
        for (int i = 0; i < num; i++) {
            recommendCode += String.valueOf(SystemConfigConstants.chars.charAt((int) (Math.random() * 26)));
        }
        return recommendCode;
    }

}
